<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Page</title>
<link href="css/template.css" rel="stylesheet">
<script type="text/javascript" src="js/jquery.min.js"></script>
<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>

<script>



	
	function loginSubmit(){
		
		var usernameErrorObject = document.getElementById("usernameError");
		usernameErrorObject.innerHTML = "";
		var passwordErrorObject = document.getElementById("passwordError");
		passwordErrorObject.innerHTML = "";
		var usernameObject = document.getElementById("username");
		var username = usernameObject.value;
		var passwordObject = document.getElementById("password");
		var password = passwordObject.value;

		if( username == "" ){
			usernameObject.focus();
			usernameErrorObject.innerHTML = "username cann't be empty!!!";
			return;
		}

		if( password == "" ){
			passwordObject.focus();
			passwordErrorObject.innerHTML = "password cann't be empty!!!";
			return;
		}
		
		jQuery.ajax({type:'post', async:false,dataType:'json', data:"username=" + username + "&password=" + password, url:'login_check.php', success:function(result){
				if( result.data.status == "success" ){
					var lastUrl = document.referrer;
					SetCookie("username", username,900);
					if( lastUrl == "" ){
						window.location.href = "index.php";
					}else{
						window.location.href = lastUrl;
					}
				}else{
					alert("username or password error, login fail!!!");
					document.getElementById("password").value = "";
				}
				
			},
			error:function(XMLHttpRequest,textStatus, errorThrown){
				alert(XMLHttpRequest.responseText);
				
			}
			
		});
	}



	//
	function SetCookie (name, value) {  
		var expdate = new Date();
		var argv = SetCookie.arguments;
		var argc = SetCookie.arguments.length;
		var expires = (argc > 2) ? argv[2] : null;
		var path = (argc > 3) ? argv[3] : null;
		var domain = (argc > 4) ? argv[4] : null;
		var secure = (argc > 5) ? argv[5] : false;
		if(expires!=null && expires>=0) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
		document.cookie = name + "=" + escape (value) +((expires == null || expires < 0) ? ((expires==-1)?"; expires=-1":"") : ("; expires="+ expdate.toGMTString()))
		+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
		+((secure == true) ? "; secure" : "");
	}


	
	window.onload=function(){
		document.onkeydown=keydown;
	}

	function keydown(evt){
		if( evt.keyCode == 13 ){
			document.getElementById("login").click();
		}
	}



	function resetInput(){
		document.getElementById("username").value = "";
		document.getElementById("password").value = "";
	}
</script>
</head>
 
<body>
		<div id="container" style="display:table;height:990px;margin:0 auto;">
			<div id="tableCellDiv" style="display:table-cell;vertical-align:middle;">
				<div id="main" style="margin:0px auto;margin-top:expression((500- this.height)/2);display:block;">
					<table style="margin:0 auto;">
						<tr>
							<td style="width:33%;text-align:right;"><label style="font-size:2em;">Username:</label></td>
							<td style="width:24%"><input type="text" name="username" id="username" style="font-size:1.2em;"value="<?php if( isset($_POST["username"])){echo $_POST["username"];}?>"/></td>
							<td style="width:33%"><label id="usernameError" style="font-size:1.5em;"></label></td>
						</tr>
						<tr>
							<td style="width:33%;text-align:right;"><label style="font-size:2em;">Password:</label></td>
							<td><input type="password" name="password" id="password" style="font-size:1.2em;"/></td>
							<td style="text-align:left;"><label id="passwordError" style="font-size:1.5em;"></label></td>
						</tr>
						<tr>
							<td></td>
							<td><input type="button" value="Login" onclick="loginSubmit()" id="login" style="width:4em; height:2em;font-size:1.5em"/></td>
							<td></td>
						</tr>
					
					
					
						
					</table>
				</div>
			</div>
		</div>
</body>

</html>
